package com.nhc.activity_fragment;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.astrob.activitys.AppContext;
import com.astrob.nhcmap.R;
import com.nhc.model.NaviTrackSetInfo;
import com.nhc.model.PoiCollectInfo;

import zuo.biao.library.base.BaseActivity;
import zuo.biao.library.ui.BottomMenuWindow;
import zuo.biao.library.ui.EditTextInfoWindow;
import zuo.biao.library.ui.TopMenuWindow;

public class PoiCollectionActivity extends BaseActivity implements View.OnClickListener {
    private final String TAG = "PoiCollectionActivity";

    //启动方法<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

    /**
     * 启动这个Activity的Intent
     *
     * @param context
     * @return
     */
    public static Intent createIntent(Context context) {
        return new Intent(context, PoiCollectionActivity.class);
    }

    //启动方法>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    @Override
    public Activity getActivity() {
        return this;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.poicollection_activity);

        //功能归类分区方法，必须调用<<<<<<<<<<
        initView();
        initData();
        initEvent();
        //功能归类分区方法，必须调用>>>>>>>>>>

    }

    //UI显示区(操作UI，但不存在数据获取或处理代码，也不存在事件监听代码)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    ImageView ivzidongcaiji;


    private TextView tv_ntmode;
    private TextView tv_ntintervel;

    private RadioGroup rg_qiya;
    private RadioButton radioButton_0;
    private RadioButton radioButton_1;

    @Override
    public void initView() {//必须调用
        ivzidongcaiji = (ImageView) findViewById(R.id.ivzidongcaiji);

        tv_ntmode = (TextView) findViewById(R.id.tv_ntmode);
        tv_ntintervel = (TextView) findViewById(R.id.tv_ntintervel);

        rg_qiya = (RadioGroup) findViewById(R.id.rg_qiya);
        radioButton_0 = (RadioButton) findViewById(R.id.radioButton_0);
        radioButton_1 = (RadioButton) findViewById(R.id.radioButton_1);
        rg_qiya.setOnCheckedChangeListener(new MyRadioButtonListener());
    }

    //UI显示区(操作UI，但不存在数据获取或处理代码，也不存在事件监听代码)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    //Data数据区(存在数据获取或处理代码，但不存在事件监听代码)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

    int _autocollect = 0;
    int _savemode = 1;

    @Override
    public void initData() {//必须调用
        _autocollect = PoiCollectInfo.getInstance().getAutocollect();
        _savemode = PoiCollectInfo.getInstance().getSavemode();
        setZidongcaiji(_autocollect);

        if (_savemode == 0) {
            radioButton_0.setChecked(true);
            radioButton_1.setChecked(false);
            radioButton_0.requestFocus();
        } else {
            radioButton_0.setChecked(false);
            radioButton_1.setChecked(true);
            radioButton_1.requestFocus();
        }

        refresh();
    }

    //Data数据区(存在数据获取或处理代码，但不存在事件监听代码)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    //Event事件区(只要存在事件监听代码就是)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

    @Override
    public void initEvent() {//必须调用
        findViewById(R.id.llSetting_2).setOnClickListener(this);
        findViewById(R.id.navitrackset_0).setOnClickListener(this);
        findViewById(R.id.navitrackset_1).setOnClickListener(this);
    }

    private int[] CTRL_IDS = {
            R.id.navitrackset_0, R.id.navitrackset_1, R.id.llSetting_2};

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        switch (keyCode) {
            default:
                break;
            case 21://left
            case 22://right
            case 19://up
            case 20://down
            case 23://ok
                setFoucseCtrl();
                break;
        }
        return super.onKeyUp(keyCode, event);
    }

    boolean _isFirst = true;

    private void setFoucseCtrl() {
        if (!_isFirst) {
            return;
        }

        _isFirst = false;

        for (int i = 0; i < CTRL_IDS.length; i++) {
            findViewById(CTRL_IDS[i]).setFocusable(true);
        }
    }

    class MyRadioButtonListener implements RadioGroup.OnCheckedChangeListener {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            // 选中状态改变时被触发
            switch (checkedId) {
                case R.id.radioButton_0:
                    _savemode = 0;
                    break;
                case R.id.radioButton_1:
                    _savemode = 1;
                    break;
            }
        }
    }

    //系统自带监听方法<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    void setZidongcaiji(int autoSave) {
        _autocollect = autoSave;
        if (_autocollect == 0) {
            ivzidongcaiji.setImageResource(R.drawable.swith_close);
            findViewById(R.id.ll_autocollect).setVisibility(View.GONE);
        } else {
            ivzidongcaiji.setImageResource(R.drawable.swith_open);
            findViewById(R.id.ll_autocollect).setVisibility(View.VISIBLE);
        }
    }

    //类相关监听<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

    @Override
    public void finish() {
        PoiCollectInfo.getInstance().setAutocollect(_autocollect);
        PoiCollectInfo.getInstance().setSavemode(_savemode);
        PoiCollectInfo.getInstance().save(this);
        super.finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.llSetting_2:
                if (_autocollect == 0) {
                    _autocollect = 1;
                } else {
                    _autocollect = 0;
                }
                setZidongcaiji(_autocollect);
                PoiCollectInfo.getInstance().setAutocollect(_autocollect);
                refresh();
                break;
            case R.id.navitrackset_0:
                toActivity(BottomMenuWindow.createIntent(context, PoiCollectInfo.COLLECTMODE_NAMES)
                        .putExtra(BottomMenuWindow.INTENT_TITLE, getString(R.string.jlms)), REQUEST_TO_BOTTOM_MENU_1, false);
                break;
            case R.id.navitrackset_1:
                if (PoiCollectInfo.getInstance().getAutocollect() == 1) {
                    toActivity(BottomMenuWindow.createIntent(context, PoiCollectInfo.INTERVALSTR_TIMES)
                            .putExtra(BottomMenuWindow.INTENT_TITLE, getString(R.string.jljg)), REQUEST_TO_BOTTOM_MENU_2, false);
                } else if (PoiCollectInfo.getInstance().getAutocollect() == 2) {
                    toActivity(BottomMenuWindow.createIntent(context, PoiCollectInfo.INTERVALSTR_DISTANCES)
                            .putExtra(BottomMenuWindow.INTENT_TITLE, getString(R.string.jljg)), REQUEST_TO_BOTTOM_MENU_2, false);
                } else {
                    showShortToast(getString(R.string.zdwxszjljg));
                }
                break;
            default:
                break;
        }
    }

    void refresh() {
        tv_ntmode.setText(PoiCollectInfo.getInstance().getCollectmodeStr());
        tv_ntintervel.setText(PoiCollectInfo.getInstance().getIntervalStr());

    }

    private final int REQUEST_TO_BOTTOM_MENU_1 = 31;
    private final int REQUEST_TO_BOTTOM_MENU_2 = 32;

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode != RESULT_OK) {
            return;
        }
        switch (requestCode) {
            case REQUEST_TO_BOTTOM_MENU_1:
                if (data != null) {
                    int selectedPosition = data.getIntExtra(BottomMenuWindow.RESULT_ITEM_ID, -1);
                    PoiCollectInfo.getInstance().setAutocollect(selectedPosition+1);
                    _autocollect = selectedPosition+1;
                    refresh();
                }
                break;
            case REQUEST_TO_BOTTOM_MENU_2:
                if (data != null) {
                    int selectedPosition = data.getIntExtra(BottomMenuWindow.RESULT_ITEM_ID, -1);
                    PoiCollectInfo.getInstance().setIntervalmode(selectedPosition);
                    refresh();
                }
                break;
        }
    }
    //类相关监听>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    //系统自带监听方法>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    //Event事件区(只要存在事件监听代码就是)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    //内部类,尽量少用<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<


    //内部类,尽量少用>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

}
